home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / cd-sup / classact / examples / label / labelexample.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  6KB  |  247 lines

  1.  
  2. /**
  3.  **  LabelExample.c -- Label class example.
  4.  **
  5.  **  This is a simple example testing some of the capabilities of the
  6.  **  Label image class.
  7.  **
  8.  **  This code opens a simple window and then creates a Label image,
  9.  **  combining text, font changes and images.  It then draws the image in
  10.  **  both its regular and selected state.
  11.  **
  12.  **  Note that we are not using window or layout class here, we are 
  13.  **  using the gadget in a fairly direct form, but that's perfectly legal.
  14.  **
  15.  **/
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/imageclass.h>
  21. #include <libraries/gadtools.h>
  22. #include <libraries/locale.h>
  23. #include <utility/tagitem.h>
  24. #include <clib/dos_protos.h>
  25. #include <clib/exec_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/locale_protos.h>
  29. #include <images/label.h>
  30. #include <proto/label.h>
  31. #include <stdio.h>
  32.  
  33. struct ClassLibrary *LabelBase;
  34.  
  35. struct ClassLibrary * OpenClass(STRPTR, ULONG);
  36.  
  37.  
  38. __chip UWORD image_data[] =
  39. {
  40.     /* Plane 0 */
  41.     0x0000, 0x0000,
  42.     0x7F00, 0x0000,
  43.     0x4180, 0x0000,
  44.     0x4140, 0x0000,
  45.     0x4120, 0x4000,
  46.     0x41F0, 0x6000,
  47.     0x401B, 0xF000,
  48.     0x401B, 0xF800,
  49.     0x401B, 0xF000,
  50.     0x4018, 0x6000,
  51.     0x4018, 0x4000,
  52.     0x4018, 0x0000,
  53.     0x4018, 0x0000,
  54.     0x7FF8, 0x0000,
  55.     0x1FF8, 0x0000,
  56.     0x0000, 0x0000,
  57.     /* Plane 1 */
  58.     0x0000, 0x0000,
  59.     0x0000, 0x0000,
  60.     0x3E00, 0x0000,
  61.     0x3E80, 0x0000,
  62.     0x3EC0, 0x0000,
  63.     0x3E00, 0x0000,
  64.     0x3FE0, 0x0000,
  65.     0x3FE0, 0x0000,
  66.     0x3FE0, 0x0000,
  67.     0x3FE0, 0x0000,
  68.     0x3FE0, 0x0000,
  69.     0x3FE0, 0x0000,
  70.     0x3FE0, 0x0000,
  71.     0x0000, 0x0000,
  72.     0x0000, 0x0000,
  73.     0x0000, 0x0000
  74. };
  75.  
  76. struct Image image =
  77. {
  78.     0, 0, 22, 16, 2, image_data, 0x03, 0x00, NULL
  79. };
  80.  
  81. struct TextAttr emerald17 = { (STRPTR)"emerald.font", 18, FS_NORMAL, 0x01 };
  82.  
  83.  
  84. /* This is the start of our programme.
  85.  */
  86. main()
  87. {
  88.     struct Screen *screen = NULL;
  89.  
  90.     /* We'll just open up on the default public screen, and use its screen font.
  91.      */
  92.     if (screen = LockPubScreen(NULL))
  93.     {
  94.         struct DrawInfo *drinfo = NULL;
  95.  
  96.         if (drinfo = GetScreenDrawInfo(screen))
  97.         {
  98.             /* Open the BOOPSI class library.
  99.              */
  100.             PutStr("Opening class\n");
  101.  
  102.             if (LabelBase = OpenClass("images/label.image", 0))
  103.             {
  104.                 struct Image *label_image;
  105.                 UWORD mapping[4];
  106.  
  107.                 /* Setup the mapping.  This is a pretty standard map for
  108.                  * a 4 colour image.
  109.                  */
  110.                 mapping[0] = drinfo->dri_Pens[BACKGROUNDPEN];
  111.                 mapping[1] = drinfo->dri_Pens[SHADOWPEN];
  112.                 mapping[2] = drinfo->dri_Pens[SHINEPEN];
  113.                 mapping[3] = drinfo->dri_Pens[FILLPEN];
  114.  
  115.                 /* Create a label image.  Here we make use of underscoring,
  116.                  * multiple lines, images, different pens, different fonts
  117.                  * and right justification.
  118.                  */
  119.                 PutStr("Creating object\n");
  120.                 if (label_image = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  121.                                                     IA_Font, screen->Font,
  122.                                                     LABEL_Justification, LABEL_CENTRE,
  123.                                                     LABEL_Text, "_Under-scored\nNot under-scored\n",
  124.                                                     LABEL_Text, "An image: ",
  125.                                                     LABEL_Mapping, mapping,
  126.                                                     LABEL_Image, &image,
  127.                                                     IA_FGPen, 3,
  128.                                                     IA_Font, &emerald17,
  129.                                                     LABEL_Text, "\nChange fonts,\n",
  130.                                                     IA_FGPen, 2,
  131.                                                     IA_Font, screen->Font,
  132.                                                     LABEL_Text, "and colours, ",
  133.                                                     LABEL_SoftStyle, FSF_BOLD,
  134.                                                     LABEL_Text, " and styles",
  135.                                                     TAG_END))
  136.                 {
  137.                     struct Window *win = NULL;
  138.  
  139.                     Printf("Image size -- width: %ld  height: %ld\n",
  140.                         (LONG)label_image->Width, (LONG)label_image->Height);
  141.  
  142.                     /* Open a simple window.
  143.                      */
  144.                     if (win = OpenWindowTags(NULL,
  145.                         WA_Left, 0,
  146.                         WA_Top, screen->Font->ta_YSize + 3,
  147.                         WA_InnerWidth, label_image->Width + INTERWIDTH * 2,
  148.                         WA_InnerHeight, (label_image->Height + INTERHEIGHT * 2) * 2,
  149.                         WA_IDCMP, IDCMP_GADGETUP | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  150.                         WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  151.                                     WFLG_SIZEGADGET | WFLG_ACTIVATE | WFLG_SMART_REFRESH,
  152.                         WA_Title, "Label Class Demo",
  153.                         WA_MinWidth, label_image->Width + INTERWIDTH * 2 + 20,
  154.                         WA_MinHeight, (label_image->Height + INTERHEIGHT * 2) * 2 + screen->Font->ta_YSize + 10,
  155.                         WA_MaxWidth, -1,
  156.                         WA_MaxHeight, -1,
  157.                         TAG_DONE))
  158.                     {
  159.                         struct RastPort *rport = win->RPort;
  160.                         struct IntuiMessage *imsg;
  161.                         UWORD top = win->BorderTop + INTERHEIGHT;
  162.                         UWORD left = win->BorderLeft + INTERWIDTH;
  163.                         BOOL ok = TRUE;
  164.  
  165.                         PutStr("Drawing image\n");
  166.  
  167.                         DrawImageState(rport, label_image, left, top,
  168.                             IDS_NORMAL, drinfo);
  169.                         DrawImageState(rport, label_image, left, top + label_image->Height + INTERHEIGHT,
  170.                             IDS_SELECTED, drinfo);
  171.  
  172.                         /* Just wait around until the close gadget is pressed.
  173.                          */
  174.                         while (ok)
  175.                         {
  176.                             WaitPort(win->UserPort);
  177.                             while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  178.                             {
  179.                                 switch(imsg->Class)
  180.                                 {
  181.                                     case IDCMP_REFRESHWINDOW:
  182.                                         DrawImageState(rport, label_image, left, top,
  183.                                             IDS_NORMAL, drinfo);
  184.                                         DrawImageState(rport, label_image, left, top + label_image->Height + INTERHEIGHT,
  185.                                             IDS_SELECTED, drinfo);
  186.                                         break;
  187.  
  188.                                     case IDCMP_CLOSEWINDOW:
  189.                                         ok = FALSE;
  190.                                         break;
  191.  
  192.                                     default:
  193.                                         break;
  194.                                 }
  195.                                 ReplyMsg((struct Message *)imsg);
  196.                             }
  197.                         }
  198.                         /* Done.
  199.                          */
  200.                         CloseWindow(win);
  201.                     }
  202.                     else
  203.                         PutStr("ERROR: Couldn't open window\n");
  204.  
  205.                     PutStr("Disposing image\n");
  206.                     DisposeObject(label_image);
  207.                 }
  208.                 else
  209.                     PutStr("ERROR: Couldn't create image\n");
  210.  
  211.                 PutStr("Closing class\n");
  212.                 CloseLibrary((struct Library *)LabelBase);
  213.             }
  214.             else
  215.                 PutStr("ERROR: Couldn't open class\n");
  216.  
  217.             FreeScreenDrawInfo(screen, drinfo);
  218.         }
  219.         else
  220.             PutStr("ERROR: Couldn't get DrawInfo\n");
  221.  
  222.         UnlockPubScreen(0, screen);
  223.     }
  224.     else
  225.         PutStr("ERROR: Couldn't lock public screen\n");
  226. }
  227.  
  228.  
  229. /* Function to open a BOOPSI class library.
  230.  */
  231. struct ClassLibrary * OpenClass(STRPTR name, ULONG version)
  232. {
  233.     struct Library *retval;
  234.     UBYTE buffer[256];
  235.  
  236.     if ((retval = OpenLibrary(name, version)) == NULL)
  237.     {
  238.         sprintf (buffer, ":classes/%s", name);
  239.         if ((retval = OpenLibrary(buffer, version)) == NULL)
  240.         {
  241.             sprintf(buffer, "classes/%s", name);
  242.             retval = OpenLibrary(buffer, version);
  243.         }
  244.     }
  245.     return((struct ClassLibrary *)retval);
  246. }
  247.